<%@ page language="java"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="security"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytags"%>
<%@ taglib prefix="mytags2"
	uri="http://www.classicnetwork.net/tags/mytags"%>

<script type="text/javascript">
  var rowCount = 10 + ${fn:length(command.images)};
 
    function addFile() {
        var table = document.getElementById('images');
      var tbody = table.getElementsByTagName("tbody")[0];
      
      var row = document.createElement("TR");
      var cell = document.createElement("TD");
      var input = document.createElement("INPUT"); 
      input.setAttribute("type","file");
      input.setAttribute("name","images["+rowCount+"].file");
      cell.appendChild(input);
      var hidden = document.createElement("INPUT"); 
      hidden.setAttribute("type","hidden");
      hidden.setAttribute("name","h_images["+rowCount+"]");
      cell.appendChild(hidden);

      row.setAttribute("id","file-"+rowCount);
      row.appendChild(cell);
      tbody.appendChild(row);
      
      rowCount++;
    }
    
    function removeFile(i) {
      var tbody = document.getElementById('images').getElementsByTagName("tbody")[0];
      tbody.removeChild(document.getElementById("file-"+i));
      rowCount--;
    }
</script>


<security:authorize ifAllGranted="ROLE_SUPER_ADMIN">
	<table cellspacing="0" width="100%">
		<tr>
			<td align="right">Created: <fmt:formatDate
				pattern="dd/MM/yyyy HH:mm" value="${command.createdDate}" /></td>
		</tr>
		<tr>
			<td align="right">Last saved: <fmt:formatDate
				pattern="dd/MM/yyyy HH:mm" value="${command.lastSavedDate}" /></td>
		</tr>
		<tr>
			<td align="right">Created by: ${command.createdBy}</td>
		</tr>
	</table>
</security:authorize>

<form:form enctype="multipart/form-data" action="carForm.html">
	<u><b>Specification:</b></u>
	<form:hidden path="id" />
	<table cellspacing="5" width="100%">
		<tr>
			<td><span class="attribute">Published:</span></td>
			<td><form:checkbox path="published" /></td>
			<td><security:authorize ifAllGranted="ROLE_ADMIN">
				<span class="attribute">Site:</span>
			</security:authorize></td>
			<td><security:authorize ifAllGranted="ROLE_ADMIN">
				<form:select cssErrorClass="error" path="site"
					cssStyle="width:137px">
					<form:option value="" label="-- select --" />
					<form:options items="${cache.codes.siteList}" itemLabel="name"
						itemValue="id" />
				</form:select>
			</security:authorize></td>
		</tr>
		<tr>
			<td><span class="attribute">Make*:</span></td>
			<td><form:select cssErrorClass="error" path="make"
				cssStyle="width:137px">
				<form:option value="" label="-- select --" />
				<form:options items="${cache.codes.makeList}" itemLabel="name"
					itemValue="id" />
			</form:select></td>
			<td><span class="attribute">Type:</span></td>
			<td><form:select cssErrorClass="error" path="type"
				cssStyle="width:137px">
				<form:option value="" label="-- select --" />
				<form:options items="${cache.codes.typeList}" itemLabel="name"
					itemValue="id" />
			</form:select></td>
		</tr>
		<tr>
			<td><span class="attribute">Model*:</span></td>
			<td><form:input cssErrorClass="error" path="model" /></td>
			<td><security:authorize ifAllGranted="ROLE_ADMIN">
				<span class="attribute">Contact:</span>
			</security:authorize></td>
			<td><security:authorize ifAllGranted="ROLE_ADMIN">
				<form:select cssErrorClass="error" path="contact"
					cssStyle="width:137px">
					<form:option value="" label="-- select --" />
					<c:forEach items="${cache.contactList}" var="contact">
						<form:option value="${contact.id}"
							label="${contact.lastName} ${contact.firstName}" />
					</c:forEach>
				</form:select>
				<c:if test="${not empty command.contact}">
					<a
						href="<c:url value="/private/contactForm.html"><c:param name="id" value="${command.contact.id}" /></c:url>">see</a>
				</c:if>
			</security:authorize></td>
		</tr>
		<tr>
			<td><span class="attribute">Drive*:</span></td>
			<td><form:input cssErrorClass="error" path="drive" /></td>
			<td><span class="attribute">Location:</span></td>
			<td><form:select cssErrorClass="error" path="location"
				cssStyle="width:137px">
				<form:option value="" label="-- select --" />
				<form:options items="${cache.codes.locationList}" itemLabel="name"
					itemValue="id" />
			</form:select></td>
		</tr>
		<tr>
			<td><span class="attribute">Year*:</span></td>
			<td><form:input cssErrorClass="error" path="year" /></td>
			<td><span class="attribute">CV:</span></td>
			<td><form:input cssErrorClass="error" path="cv" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Sales Price:</span></td>
			<td><input type="text" name="alternativeTextPrice"
				value="${(not empty command.alternativeTextPrice )? command.alternativeTextPrice : command.salesPrice}" />
			</td>
			<td><span class="attribute">Engine ref:</span></td>
			<td><form:input cssErrorClass="error" path="engineNumber" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Agreed Price:</span></td>
			<td><form:input cssErrorClass="error" path="agreedPrice" /></td>
			<td><span class="attribute">Interior:</span></td>
			<td><form:input cssErrorClass="error" path="interior" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Chassis ref:</span></td>
			<td><form:input cssErrorClass="error" path="chassisNumber" /></td>
			<td><span class="attribute">KMs:</span></td>
			<td><form:input cssErrorClass="error" path="kms" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Engine (cc):</span></td>
			<td><form:input cssErrorClass="error" path="cc" /></td>
			<td><span class="attribute">Color Exterior:</span></td>
			<td><form:input cssErrorClass="error" path="colorExterior" /></td>
		</tr>
	</table>

	<table cellpadding="10" width="100%" align="center">
		<tr>
			<td width="50%"><u><b>Options:</b></u>
			<table>
				<c:forEach items="${cache.codes.optionList}" var="option">
					<tr>
						<td><input type="checkbox" name="opts" value="${option.id}"
							<mytags2:checked option="${option}" /> /></td>
						<td>${option.name}</td>
					</tr>
				</c:forEach>
			</table>
			</td>
			<td width="50%" valign="top"><u><b>Description:</b></u> <br />
			<form:textarea cols="50" rows="20" path="description" />
			<p><u><b>Comment (not displayed at the client):</b></u> <br />
			<form:textarea cols="50" rows="20" path="comment" />
			</td>
		</tr>
	</table>
	<p />
	<table>
		<tr>
			<td class="info" width="400">To change the displayed thumb nail,
			check the radio button (<input type="radio" />) near the image you
			want</td>
		</tr>
	</table>
	<p />
	<table>
		<tr>
			<td class="info" width="400">To delete one or more images,
			select the check boxes (<input type="checkbox" />) near the images
			you want to delete. The image used for thumbnail will not be deleted</td>
		</tr>
	</table>
	<p><u><b>Photos:</b></u>
	<p><c:if test="${not command.new}">
		<table>
			<c:forEach items="${command.images}" var="image" varStatus="i">
				<c:if test="${i.count == 1 || i.count % 5 == 1}">
					<tr>
				</c:if>
				<td><form:radiobutton path="thumb" value="${image.id}" /> <input
					type="checkbox" name="2delete" value="${image.id}" /> <a
					href="<c:url value="/viewCarImages.html" ><c:param name="id" value="${command.id}" /><c:param name="imageId" value="${image.id}" /></c:url>"
					target="_blank"><mytags:image id="${image.id}" cssClass="thumb" /></a></td>
				<c:if test="${i.count % 5 == 0}">
					</tr>
				</c:if>
			</c:forEach>
		</table>
	</c:if>
	<p>
	<table id="images" width="100%">
		<c:forEach begin="0" end="9" varStatus="rowCount">
			<tr>
				<td><input type="file" name="uploadImages[${rowCount.index}].file" />
				<input type="hidden" name="h_images[${rowCount.index}]" /></td>
			</tr>
		</c:forEach>
	</table>

	<br>
	<a href="javascript:addFile()"><b>Attach an Image</b></a>
	<br>

	<p /><input type="submit" name="action" value="save" class="bouton" />&#0160;
	<security:authorize ifAllGranted="ROLE_ADMIN">
		<input type="submit" name="action" value="email" class="bouton" />
	</security:authorize>
</form:form>
